package com.turtle3d.exporter;

import java.util.ArrayList;
import java.util.Hashtable;

/**
 * <p>A data structure for vertices, normals and materials.</p>
 * 
 * <p>It's an array that doesn't allow duplicates.</p>
 * 
 * @author Marek Paterczyk <marek.paterczyk@gmail.com>
 *
 */
public class ArrayHash<E>
{
	private Hashtable<E, Integer> hashtable = new Hashtable<E, Integer>();
	private ArrayList<E> arraylist = new ArrayList<E>();
	private boolean allowDuplicates=false;
	
	
	public ArrayHash(boolean allowDuplicates) {		
		this.allowDuplicates = allowDuplicates;
	}
	
	public ArrayHash(){}

	public int add(E object){
		if(!allowDuplicates){
			if(hashtable.containsKey(object))
				return hashtable.get(object);
			
			int pos = arraylist.size()+1;
			arraylist.add(object);
			hashtable.put(object, pos);
			
			return pos;
		} else {
			int pos = arraylist.size()+1; 
			arraylist.add(object);
			hashtable.put(object, pos);
			return pos;
		}
		
		
	}
	
	public Integer getIndex(E object){		
		return hashtable.get(object);
	}

	public ArrayList<E> getArraylist()
	{
		return arraylist;
	}
		

}
